package com.shantoo.android.rongcloud.master.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.shantoo.android.rongcloud.master.Constans;
import com.shantoo.android.rongcloud.master.model.Friend;
import com.shantoo.android.rongcloud.master.R;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.UserInfo;

/**
 * 描述: 这里链接融云 设置用户
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener, RongIM.UserInfoProvider{

    private static final String TAG = "MainActivity";

    private List<Friend> userIdList;

    private Button mUser1, mUser2;

    private String currentUserId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mUser1 = (Button) findViewById(R.id.connect_10010);
        mUser2 = (Button) findViewById(R.id.connect_10086);
        mUser1.setOnClickListener(this);
        mUser2.setOnClickListener(this);
        findViewById(R.id.action_single_chat).setOnClickListener(this);
        findViewById(R.id.action_chat_list).setOnClickListener(this);
        findViewById(R.id.action_service).setOnClickListener(this);
    }

    private void connect(String token) {
        RongIM.connect(token, new RongIMClient.ConnectCallback() {
            @Override
            public void onSuccess(String userId) {
                if (userId.equals("1710")) {
                    mUser1.setText("用户1710连接融云服务器成功");
                    mUser2.setText("点击连接融云server（用户2）");
                    if("61".equals(currentUserId)){
                        Toast.makeText(MainActivity.this, "切换用户登录成功", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    }
                    currentUserId = "1710";
                } else if(userId.equals("61")){
                    mUser2.setText("用户61连接融云服务器成功");
                    mUser1.setText("点击连接融云server（用户1）");
                    if("1710".equals(currentUserId)){
                        Toast.makeText(MainActivity.this, "切换用户登录成功", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    }
                    currentUserId = "61";
                }
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {
                Log.e(TAG, "connect failure errorCode is : " + errorCode.getValue());
            }

            @Override
            public void onTokenIncorrect() {
                Log.e(TAG, "token is error ,please check token and app key");
            }
        });
    }

    public void onClick(View v) {
        if (v != null) {
            switch (v.getId()) {
                case R.id.connect_10010:
                    connect(Constans.TOKEN1);
                    break;
                case R.id.connect_10086:
                    connect(Constans.TOKEN2);
                    break;
                case R.id.action_single_chat://会话
                    actionSingleChat();
                    break;
                case R.id.action_chat_list://会话列表
                    actionChatList();
                    break;
                case R.id.action_service://客服
                    actionService();
                    break;
            }
        }
    }

    //单聊
    private void actionSingleChat(){
        String targetUserId = null;
        String title = null;
        if("1710".equals(currentUserId)){
            targetUserId = "61";
            title = "美女";
        }else if("61".equals(currentUserId)){
            targetUserId = "1710";
            title = "帅哥";
        }

        Log.e("targetUserId",targetUserId);
        if (RongIM.getInstance() != null) {
            RongIM.getInstance().startPrivateChat(this, targetUserId, title);
        }
    }

    //会话列表
    public void actionChatList(){
        initUserInfo();
        startActivity(new Intent(MainActivity.this, ConversationListActivity.class));
    }

    //客服
    public void actionService(){
        if (RongIM.getInstance() != null) {
            RongIM.getInstance().startConversation(this, Conversation.ConversationType.APP_PUBLIC_SERVICE, "1", "客服");
        }
    }

    /**
     * 这里的数据从服务器获取，用來跟新会话列表的信息，
     * */
    private void initUserInfo() {
        userIdList = new ArrayList<>();
        userIdList.add(new Friend("1710", "用户1710", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1517912302298&di=0b39cb7f870c4fc0c89a2c2dee28c758&imgtype=0&src=http%3A%2F%2Fwww.qqpk.cn%2FArticle%2FUploadFiles%2F201010%2F2010101323493651.jpg"));
        userIdList.add(new Friend("61", "用户61", "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1981113993,1464442148&fm=27&gp=0.jpg"));
        userIdList.add(new Friend("1", "在线客服", "http://img02.tooopen.com/Download/2010/5/22/20100522103223994012.jpg"));
        RongIM.setUserInfoProvider(this, true);
    }

    @Override
    public UserInfo getUserInfo(String s) {
        for (Friend i : userIdList) {
            if (i.getUserId().equals(s)) {
                Log.e(TAG, i.getPortraitUri());
                return new UserInfo(i.getUserId(), i.getName(), Uri.parse(i.getPortraitUri()));
            }
        }
        Log.e(TAG, "UserId is : " + s);
        return null;
    }
}
